// components/cn-qrcode/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: true
    },
    codeImgUrl: {
      type: String,
      value: ''
    },
    top: {
      type: String,
      value: '54%',
    },
    /**二维码名称 */
    title: {
      type: String,
      value: '分享码'
    },
    width: {
      type: String,
      value: '100%'
    },
    height: {
      type: String,
      value: undefined
    },
    minHeight: {
      type: String,
      value: undefined
    },
    showTitle: {
      type: Boolean,
      value: true,
    },
    /** 点击蒙层是否允许关闭	 */
    maskClosable: {
      type: Boolean,
      value: true,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的方法列表
   */
  methods: {
    //判断用户是否授权"保存到相册"
    checkSetting(e) {
      let _self = this;
      const {
        type
      } = e.currentTarget.dataset;
      wx.getSetting({
        success(res) {
          //没有权限，发起授权
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() { //用户允许授权，保存图片到相册
                _self.downloadUrl(type);
              },
              fail() { //用户点击拒绝授权，跳转到设置页，引导用户授权
                wx.navigateTo({
                  url: '/pages/base/status/status?type=setting&status=2'
                })
              }
            })
          } else { //用户已授权，保存到相册
            _self.downloadUrl(type);
          }
        }
      })
    },
    //预下载或预保存
    async downloadUrl(type) {
      let _self = this,
        {
          codeImgUrl,
          codeImgAliPay
        } = _self.data,
        codeDown = wx.getFileSystemManager(),
        filePath = '',
        data = '',
        encoding = 'base64';

      switch (type) {
        case '0':
          data = codeImgUrl;
          filePath = wx.env.USER_DATA_PATH + '/小程序码.png';
          break;
        case '1':
          data = await _self.transUrlImgToBase64(codeImgAliPay);
          filePath = wx.env.USER_DATA_PATH + '/支付宝二维码.png';
          break;
      }

      codeDown.writeFile({
        filePath,
        data,
        encoding,
        success: () => {
          _self.saveImage(filePath);
        }
      })
    },
    //保存到本地
    saveImage(filePath) {
      wx.saveImageToPhotosAlbum({ //保存到本地
        filePath,
        success() {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 2000
          })
        },
        fail: function () {
          wx.showToast({
            title: '保存失败',
            icon: 'none'
          })
        }
      })
    },
    /** 小程序码 */
    showPic() {
      const {
        codeImgUrl
      } = this.data,
        src = 'data:image/png;base64,' + codeImgUrl,
        imgList = [src];
      if (codeImgUrl) {
        wx.previewImage({
          current: src, // 当前显示图片的http链接 
          urls: imgList // 需要预览的图片http链接列表如果没有预览图片指向原图片地址即可
        });
      }
    },
    close() {
      this.setData({
        show: false
      })
    },
    hide() {
      this.close()
    },
    doNothing() {},
    shareCodeImg(e) {
      this.triggerEvent('shareCodeImg', e.detail)
    },
  },
})